<template>
  <div>
    <el-dialog :title="info.isAdd?'角色添加':'角色编辑'" :visible.sync="info.isshow" @closed="closed">
      {{form}}
      <el-form :model="form">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="角色权限" label-width="120px">
          <el-tree
            :data="menuList"
            :props="{children: 'children',label: 'title',}"
            node-key="id"
            ref="tree"
            show-checkbox
            default-expand-all
          ></el-tree>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqmenulist,
  reqroleadd,
  reqroleedit,
  reqroleinfo,
} from "../../../request/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  components: {},
  props: ["info", "list"],
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: "",
      },
      menuList: [],
    };
  },
  mounted() {
    reqmenulist({ istree: true }).then((res) => {
      if (res.data.code == 200) {
        this.menuList = res.data.list;
      }
    });
  },
  computed: {
    ...mapGetters({
      user: "user",
    }),
  },
  methods: {
    ...mapActions({
      useractions: "useractions",
    }),
    cancel() {
      this.info.isshow = false;
    },
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    empty() {
      this.form = {
        rolename: "",
        menus: [],
        status: "",
      };

      this.$refs.tree.setCheckedKeys([]);
    },
    checkEmpty() {
      return new Promise((resolve, reject) => {
        if (this.form.rolename == "") {
          errorAlert("角色名称不能为空");
          return;
        }
        if (this.$refs.tree.getCheckedKeys().length == 0) {
          errorAlert("角色权限不能为空");
          return;
        }
        resolve();
      });
    },
    //添加
    add() {
      this.checkEmpty().then(() => {
        this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
        reqroleadd(this.form).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);

            this.cancel();

            this.empty();

            this.$emit("init");
          }
        });
      });
    },

    changePid() {
      if (this.form.pid == 0) {
        this.form.type = 1;
      } else {
        this.form.type = 2;
      }
    },

    getOne(id) {
      reqroleinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;

          this.form.id = id;

          this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus));
        }
      });
    },
    //修改
    update() {
      this.checkEmpty().then(() => {
        this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
        reqroleedit(this.form).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);
            if (this.form.id === this.user.roleid) {
              this.useractions({});
              this.$router.replace("/login");
              return;
            }
            this.cancel();
            this.empty();
            this.$emit("init");
          }
        });
      });
    },
  },
};
</script>
<style lang="less" scoped>
</style>